// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'keepfit-cloud',
  traceUser: true
})

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  let tmp = await db.collection('User').where({
    openid:event.openid
  }).get()
  if (tmp.data.length == 0){
    return await db.collection('User').add({
      data: {
        name: event.nickName,
        city: event.city,
        country: event.country,
        gender: event.gender,
        language: event.language,
        avatarUrl: event.avatarUrl,
        openid: event.openid,//下面是非功能字段
        registerTime: String(new Date().getTime()),
        lastLogIn: String(new Date().getTime()),
        phoneNumber:"",
        isIndenti:false
      }
    })
  }else {
    return await db.collection('User').where({
      openid:event.openid
    }).update({
      data: {
        lastLogIn: String(new Date().getTime()),
      }
    })
  }
}